package com.qili.aspect;

import com.qili.core.utils.DictTypes;
import com.qili.entity.base.BaseDict;
import com.qili.service.base.BaseDictService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Auther liuyang
 * @Date
 * @Version
 */
@Slf4j
@Aspect
@Component
@SuppressWarnings({"unused"})
public class GuideAspect {

    @Autowired
    private BaseDictService baseDictService;

    @Pointcut("@annotation(com.qili.annotation.Guide)")
    public void annotationPointcut() {
    }

    @Before("annotationPointcut()")
    public void beforePointcut(JoinPoint joinPoint) {
        // 此处进入到方法前  可以实现一些业务逻辑
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String requestURI = request.getRequestURI();
        BaseDict selectBaseDict = new BaseDict();
        selectBaseDict.setBadtLevel1(requestURI);
        selectBaseDict.setBadtTypecode(DictTypes.PROJECT_MILEPOST.getType());
        Map guideMap = baseDictService.getGuideTextLen(selectBaseDict);
        Object lenObj = guideMap.get("guideLen");
        log.info("获取里程碑（url----"+requestURI+")的指南长度为"+lenObj);
        if(null == lenObj || (Long)lenObj == 0){
            request.setAttribute("haveGuide",false);
        }else{
            request.setAttribute("haveGuide",true);
            request.setAttribute("badtNo",guideMap.get("badtNo"));
        }

    }

}
